   package com.javalong.affairs.base.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.AdapterView.OnItemClickListener;

import com.javalong.affairs.R;
import com.javalong.affairs.module.notifything.adapter.NotificationAdapter;
import com.javalong.affairs.module.schedule.adapter.ScheduleAdapter;
import com.javalong.affairs.base.constant.BundleKey;
import com.javalong.affairs.base.constant.DataType;
import com.javalong.affairs.base.constant.MapKey;
import com.javalong.affairs.base.data.AllData;
import com.javalong.affairs.base.manager.IntentManager;
import com.javalong.affairs.base.model.SimpleMessage;
import com.javalong.affairs.module.notifything.activity.WatchMsgActivity;
import com.javalong.affairs.base.ui.SimplePullRefreshList;
import com.javalong.affairs.base.ui.TopTitle;
import com.javalong.affairs.base.ui.TopTitle.OnRightTextClick;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

@ContentView(R.layout.activity_messagesearched)
public class MessageSearchedActivity extends UIHandlerActivity implements
		OnRightTextClick, OnItemClickListener {
	@ViewInject(R.id.ui_toptitle)
	private TopTitle topTitle;
	@ViewInject(R.id.lv_msg)
	private SimplePullRefreshList lv_msg;
	private BaseAdapter adapter;
	private Map<String, Object> paramMap;
	private int currType;
	private List<SimpleMessage> msgList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		topTitle.setOnLeftDrawableClick(this);
		topTitle.setOnRightTextClick(this);
		Bundle bundle = getIntent().getBundleExtra(BundleKey.BUNDLE);
		paramMap = (Map<String, Object>) bundle
				.getSerializable(BundleKey.REQUESTPARAMS);
		currType = bundle.getInt(BundleKey.SEARCHTYPE, 0);
		msgList = AllData.GetInstance().appendListData(
				R.string.data_all_searched_msg, new ArrayList<SimpleMessage>());
		switch (currType) {
		case 0:
			adapter = new NotificationAdapter(
					this,
					msgList,
					NotificationAdapter.Type.Receive);
			break;
		case 1:
			adapter = new NotificationAdapter(this, msgList,
					NotificationAdapter.Type.Send);
			break;
		case 2:
			adapter = new ScheduleAdapter(this, msgList);
			break;
		}
		lv_msg.setAdapter(adapter);
		lv_msg.initData(getMapData());
		lv_msg.setOnItemClickListener(this);
		lv_msg.refresh();

	}

	private Map<String, Object> getMapData() {
		Map<String, Object> param = new HashMap<String, Object>();
		switch (currType) {
		case 0:
			param.put(MapKey.ACTION_NAME, "SendMessage");
			param.put(MapKey.METHOD_NAME, "outList_message_selectReceive");
			break;
		case 1:
			param.put(MapKey.ACTION_NAME, "SendMessage");
			param.put(MapKey.METHOD_NAME, "outList_message_selectSend");
			break;
		case 2:
			param.put(MapKey.ACTION_NAME, "Journal");
			param.put(MapKey.METHOD_NAME, "outList_journal_select");
			break;
		}

		param.put(MapKey.DATA_RESID, R.string.data_all_searched_msg);
		param.put(MapKey.DATA_TYPE, DataType.LIST_SIMPLEMESSAGE);
		param.put(MapKey.REQUEST_PARAMS, paramMap);
		return param;
	}

	@Override
	public void rightTextClick() {

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if (position < 2) {
			return;
		}
		if (msgList != null && msgList.size() > position - 2) {
			SimpleMessage msgInfo = msgList.get(position - 2);
			Bundle bundle = new Bundle();
			bundle.putString(BundleKey.MSGID, msgInfo.messageId);
			bundle.putString(BundleKey.SIMPLEMSGID, msgInfo.id);
			bundle.putString(BundleKey.SIMPLEMSGSTATUS, msgInfo.receiveStatus);
			bundle.putInt(BundleKey.SEARCHTYPE, currType);
			IntentManager.doActivity(this, WatchMsgActivity.class, bundle);
		}

	}
}
